package com.bbs.utils;

import com.alibaba.fastjson.JSON;
import com.bbs.pojo.Message;
import com.bbs.pojo.dto.MessageDto;
import com.bbs.pojo.dto.message.MessageContent;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {
    @Autowired
    RabbitTemplate rabbitTemplate;

    public void send(String receiverId, String senderId, MessageContent messageContent){
        Message message = new Message();
        message.setReceiverId(receiverId);
        message.setSenderId(senderId);
        message.setContent(JSON.toJSONString(messageContent));
        message.setType(messageContent.messageType());
        message.setAcknowledged(false);

        rabbitTemplate.convertAndSend("message_exchange", "sending", message);
    }
}
